package com.zeng.plugin.util;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.*;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FreeMarkUtil {
    private Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);

    private Template template;

    private String pojoTemplate = "template.ftl";
    private String mapperTemplate = "mapper.ftl";
    private String voTemplate = "voTemplate.ftl";
    //    private String serviceTemplate = "service.ftl";
    private String serviceTemplate = "Service.java";
    //    private String serviceimplTemplate = "serviceimpl.ftl";
    private String serviceimplTemplate = "ServiceImpl.java";
    private String controllerTemplate = "controller.ftl";
    private String wrapperTemplate = "Wrapper.java";
    private String mapperXmlTemplate = "mapperXml.ftl";

    public FreeMarkUtil(String projectUrl) throws IOException {
        StringTemplateLoader stringLoader = new StringTemplateLoader();
        stringLoader.putTemplate(pojoTemplate, IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream(pojoTemplate), "UTF-8"));
        stringLoader.putTemplate(mapperTemplate, IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream(mapperTemplate), "UTF-8"));
        stringLoader.putTemplate(voTemplate, IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream(voTemplate), "UTF-8"));
        stringLoader.putTemplate(serviceTemplate, IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream(serviceTemplate), "UTF-8"));
        stringLoader.putTemplate(serviceimplTemplate, IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream(serviceimplTemplate), "UTF-8"));
        stringLoader.putTemplate(controllerTemplate, IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream(controllerTemplate), "UTF-8"));
        stringLoader.putTemplate(wrapperTemplate, IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream(wrapperTemplate), "UTF-8"));
        stringLoader.putTemplate(mapperXmlTemplate, IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream(mapperXmlTemplate), "UTF-8"));

        cfg.setTemplateLoader(stringLoader);
    }

    public FreeMarkUtil(List<String> templates) throws IOException {
        StringTemplateLoader stringLoader = new StringTemplateLoader();
        for (String s : templates) {
            stringLoader.putTemplate(s, IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream(s)));
        }
        cfg.setTemplateLoader(stringLoader);
    }

    public FreeMarkUtil(String template, String template2) {
        StringTemplateLoader stringLoader = new StringTemplateLoader();
        stringLoader.putTemplate("tempalte", template);
        cfg.setTemplateLoader(stringLoader);
    }

    public String parse(Map<String, Object> data) {
        ByteArrayOutputStream byteOutputStream = null;
        try {
            template = cfg.getTemplate("tempalte", "UTF-8");
            byteOutputStream = new ByteArrayOutputStream();
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(byteOutputStream, StandardCharsets.UTF_8);
            template.process(data, outputStreamWriter);
            byte[] bytes = byteOutputStream.toByteArray();
            return new String(bytes, "UTF-8");
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        } finally {
            if (byteOutputStream != null) {
                try {
                    byteOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (byteOutputStream != null) {
                try {
                    byteOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public void parse(String templateName, String fileName, Map<String, Object> data) throws IOException, TemplateException {
        this.parse(templateName, new File(fileName), data);
    }

    public void parse(String templateName, File file, Map<String, Object> data) throws IOException, TemplateException {
        BufferedWriter bufferedWriter = null;
        FileOutputStream fileOutputStream = null;
        OutputStreamWriter outputStreamWriter = null;

        try {
            template = cfg.getTemplate(templateName, "UTF-8");
            fileOutputStream = new FileOutputStream(file);
            outputStreamWriter = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
            bufferedWriter = new BufferedWriter(outputStreamWriter);
            template.process(data, bufferedWriter);
        } finally {

            if (bufferedWriter != null) {
                bufferedWriter.close();
            }
            if (outputStreamWriter != null) {
                outputStreamWriter.close();
            }
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }

        }

    }

}
